<?php
class GastosController extends AppController {
	var $name = 'Gastos';
	var $components = array('RequestHandler');
	
	function index(){
		$this->layout="ajax";		
		
		if($this->JQGridPaginate->isGrid){
			$fields = array('Gasto.id',
			'"Tipodocumento"."descripcion" AS "Gasto__tipodocumento"',
			'Gasto.serie',
			'Gasto.numero','Gasto.razon','Gasto.descripcion',
			'Gasto.igv','Gasto.monto','Gasto.fecha',
			//'"Usuario"."username" AS "Gasto__usuario"',
			'"Gasto"."created"::date AS "Gasto__created"',
			'"Estado"."descripcion" AS "Gasto__estado"');
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;

			$this->autoRender = false;
			//echo $fields;
			echo $this->JQGridPaginate->paginate("Gasto");
		}elseif($this->RequestHandler->isPost()){	
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
			    $this->data['Gasto']['usuario_id'] = 1;
			    $this->data['Gasto']['organizacion_id'] = 1;
				$this->Gasto->create();				 
			}elseif ($oper == "edit"){
				$this->data['Gasto']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Gasto']['id'] = $this->params['form']['id'];
				$this->data['Gasto']['estado_id'] = 2;				
			}
			
			@$this->Gasto->save($this->data['Gasto']);
			echo json_encode($this->Gasto->visualizarError($this->Gasto->validationErrors));
			$this->autoRender = false;			
			}		
		}
}